/*
 * ChooseLocalPictureFragment.java
 * business
 *
 * Created by ChenTao on 2017/5/16.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.chooselocalpicture;

import android.app.Activity;
import android.support.v7.widget.ListPopupWindow;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.constant.Common;
import com.yidiandao.business.manager.ImageManager;
import com.yidiandao.business.ui.adapter.ChooseLocalPictureGridAdapter;
import com.yidiandao.business.ui.adapter.ChooseLocalPictureListPopAdapter;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ChooseLocalPictureGridItem;
import com.yidiandao.business.utils.SysUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/** 本地图片多选 */
public class ChooseLocalPictureFragment extends SuperBaseFragment
        implements ChooseLocalPictureContract.View {

    private ChooseLocalPictureContract.Presenter mPresenter;
    private Toolbar mToolbar;
    private ImageView mIvBack;
    private TextView mTvSure;
    private TextView mTvNum;
    private GridView mGridView1;
    private TextView mTvSelectedTitle;
    private ListPopupWindow mPopupWindow;
    private ChooseLocalPictureListPopAdapter mListPopAdapter;
    private int num; //当前已选择的图片数量
    ChooseLocalPictureGridItem.OnItemClickClass onItemClickClass =
            new ChooseLocalPictureGridItem.OnItemClickClass() {

                @Override
                public void OnItemClick(String path, CheckBox checkBox) {
                    if (checkBox.isChecked()) {
                        checkBox.setChecked(false);
                        ImageManager.getInstance().getCurrentImageList().remove(path);
                        num--;
                    } else {
                        try {
                            if (ImageManager.getInstance().getCurrentImageList().size()
                                    >= Common.PICTURESIZE) {
                                UIUtils.showToast("最多只能选择6张");
                                return;
                            }
                            checkBox.setChecked(true);
                            ImageManager.getInstance().getCurrentImageList().add(path);
                            num++;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    setSelectNum();
                }
            };
    private ChooseLocalPictureGridAdapter mGridAdapter;

    public static ChooseLocalPictureFragment getInstance() {
        return new ChooseLocalPictureFragment();
    }

    @Override
    public void setPresenter(ChooseLocalPictureContract.Presenter presenter) {

        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return ChooseLocalPictureFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mIvBack = (ImageView) mRootView.findViewById(R.id.iv_back);
        mTvSure = (TextView) mRootView.findViewById(R.id.tv_sure);
        mTvNum = (TextView) mRootView.findViewById(R.id.tv_num);
        mGridView1 = (GridView) mRootView.findViewById(R.id.gridView1);
        mTvSelectedTitle = (TextView) mRootView.findViewById(R.id.tv_selected_title);
        mGridAdapter = new ChooseLocalPictureGridAdapter(mActivity);
        mGridView1.setAdapter(mGridAdapter);
        initListPopWindow();
        initEvent();

        ImageManager.getInstance().setClickListener(onItemClickClass);
    }

    @Override
    protected void startLoadData() {
        //初始化添加数据
        List<String> finalImageList = ImageManager.getInstance().getFinalImageList();
        num = finalImageList.size();
        setSelectNum();
        ImageManager.getInstance().setCurrentImageList(finalImageList);
        mPresenter.start();
    }

    private void initListPopWindow() {
        int screenWidth = SysUtils.getScreenWidth(mActivity);
        mPopupWindow = new ListPopupWindow(mActivity);
        /* View view = View.inflate(mActivity, R.layout.pop_choose_local_picture, null);
        ListView listView = (ListView) view.findViewById(R.id.list_view);*/
        mListPopAdapter = new ChooseLocalPictureListPopAdapter(mActivity);
        mPopupWindow.setAdapter(mListPopAdapter);
        mPopupWindow.setContentWidth(screenWidth);
        mPopupWindow.setWidth(-1);
        mPopupWindow.setHeight(screenWidth);
        mPopupWindow.setAnchorView(mTvSelectedTitle);
        mPopupWindow.setModal(true);
        // 华为手机适配
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        /*  mPopupWindow.setContentView(view);
        mPopupWindow.setFocusable(true);*/
        //        mPopupWindow.setAnimationStyle(R.style.popwin_anim_style);

        mPopupWindow.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(
                            AdapterView<?> parent, View view, int position, long id) {
                        mPresenter.getIndexData(position);
                        mPopupWindow.dismiss();
                    }
                });
        mPopupWindow.setOnDismissListener(
                new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
                        params.alpha = 1f;
                        mActivity.getWindow().setAttributes(params);
                    }
                });
    }

    private void initEvent() {
        mIvBack.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mActivity.finish();
                    }
                });
        mTvSure.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ImageManager.getInstance()
                                .setFinalImageList(
                                        ImageManager.getInstance().getCurrentImageList());
                        ImageManager.getInstance()
                                .onSureImage(ImageManager.getInstance().getCurrentImageList());
                        mActivity.finish();
                    }
                });

        mTvSelectedTitle.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        /* int[] location = new int[2];
                        v.getLocationOnScreen(location);
                        mPopupWindow.showAsDropDown(v, location[0], -2 * v.getHeight() -
                        location[1]);*/
                        mPopupWindow.show();
                        //阴影背景
                        WindowManager.LayoutParams params = mActivity.getWindow().getAttributes();
                        params.alpha = 0.55f;
                        mActivity
                                .getWindow()
                                .addFlags(
                                        WindowManager.LayoutParams
                                                .FLAG_DIM_BEHIND); //此行代码主要是解决在华为手机上半透明效果无效的bug
                        mActivity.getWindow().setAttributes(params);
                    }
                });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_choose_local_pic;
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    public Activity getCurrentActivity() {
        return mActivity;
    }

    @Override
    public void setDisPlayItems(List<DisplayListItem> disPlayItems) {
        mListPopAdapter.setData(disPlayItems);
    }

    @Override
    public void setGridViewData(List<DisplayListItem> displayListItems) {
        mGridAdapter.setData(displayListItems);
    }

    @Override
    public void setSelectDirText(String filename) {
        mTvSelectedTitle.setText(filename);
    }

    private void setSelectNum() {
        mTvSure.setEnabled(num != 0);
        mTvNum.setVisibility(num == 0 ? View.GONE : View.VISIBLE);
        mTvNum.setText(num + "");
    }

    @Override
    public void onDestroy() {
        ImageManager.getInstance().setClickListener(null);
        super.onDestroy();
    }
}
